package bank;

public class _61RotateRight {
    //  只是权当练练递归吧
    public static ListNode rotateRight(ListNode head, int k) {
        if(k <= 0 || head == null || head.next == null){
            return head;
        }
        //  求出链表长度
        int len = 1;
        ListNode p = head;
        while (p.next != null){
            p = p.next;
            len++;
        }
        k %= len;
        return rotateHelper(head,k);
    }

    private static ListNode rotateHelper(ListNode head, int k) {
        if (k <= 0 || head == null){
            return head;
        }
        ListNode last = head;
        ListNode lastLast = last;
        while (last.next != null){
            lastLast = last;
            last = last.next;
        }
        if (last == head){
            return head;
        }
        last.next = head;
        lastLast.next = null;
        return rotateRight(last,k-1);
    }
}
